#include "Arena.h"
#include "TowerBase.h"
#include "Ammo.h"
#include "Wave.h"
#include "EnemyBase.h"


Arena::Arena(void)
{
}


Arena::~Arena(void)
{

	drawableObjects.removeAllElements();
	waveInteractors.removeAllElements();
	clickableObjects.removeAllElements();
	memoryHolder.deleteAllElements();

}


void Arena::drawArena(SDL_Renderer * renderer)
{
	map.draw(renderer);

	for(int i = 0;i < waves.size();i++)
	{
		waves.getElementByIndex(i)->drawAllEnemies(renderer);
	}

	drawableObjects.drawAll(renderer);

}
void Arena::updateArena()
{
	int s = waves.size();
	for(int i = 0;i < waves.size();i++)
	{
		waves.getElementByIndex(i)->updateWave();
		waves.getElementByIndex(i)->updateAllEnemies();
	}
	
	for(int j = 0;j < waves.size();j++)
	{
		waveInteractors.interactAll(waves.getElementByIndex(j));
	}

	updateableObjects.updateAll();

}

void Arena::addTower(TowerBase * tower)
{
	drawableObjects.addElement(tower);
	waveInteractors.addElement(tower);
	clickableObjects.addElement(tower);
	memoryHolder.addElement(tower);


}
void Arena::removeTower(TowerBase * tower)
{
	drawableObjects.removeElement(tower);
	waveInteractors.removeElement(tower);
	clickableObjects.removeElement(tower);
	memoryHolder.deleteElement(tower);
}

void Arena::addAmmo(Ammo * ammo)
{
	drawableObjects.addElement(ammo);
	updateableObjects.addElement(ammo);
	memoryHolder.addElement(ammo);
}
void Arena::removeAmmo(Ammo * ammo)
{
	drawableObjects.removeElement(ammo);
	updateableObjects.removeElement(ammo);
	memoryHolder.removeElement(ammo);
}

void Arena::addWave(Wave * wave)
{
	for(int i = 0;i<wave->size();i++)
	{
		wave->getElementByIndex(i)->setRoad(map.getRoadAt(0));
		wave->getElementByIndex(i)->spawn();
	}
	waves.addElement(wave);
}

void Arena::removeWave(Wave * wave)
{
	waves.removeElement(wave);
}

bool Arena::getMapTile(int x,int y)
{
	return map.getTileAt(x,y);
}


void Arena::loadMapTexture(const char * fileName)
{
	map.loadMapImage(fileName);
}

void Arena::tryInteract(int x,int y)
{
	
	for(int i = 0;i<clickableObjects.size();i++)
	{
		if(clickableObjects.getElementByIndex(i)->pointInside(x,y))
		{
			clickableObjects.getElementByIndex(i)->onClick();
		}
	}
}

void Arena::loadMapData(const char * fileName)
{
	map.loadMapData(fileName);
}
void Arena::loadRoad(const char * fileName)
{
	map.loadRoadFromFile(fileName);
}